﻿Imports System.Text.RegularExpressions

Public Class MobileBasicInfoForm

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.Text = "增加工程"
    End Sub

    Public Sub New(ByVal pi As ProjectItem)
        MyClass.New()

        _pi = pi

        Me.Text = "修改工程"
    End Sub

    Private _pi As ProjectItem = Nothing

    Private Sub ProjectBasicInfoForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        COMHelper.FillComName(Me.cboCOM)

        If _pi IsNot Nothing Then
            LoadInfo()
            LoadConnectConfig(_pi) 'Add by ZhangTeng
        End If
    End Sub

    Private Sub LoadInfo()
        Me.txtProjectName.Text = _pi.ProjectName
        Me.txtRemark.Text = _pi.Remark
        Try
            'Me.nudCOM.Value = _pi.ComNumber
            If _pi.connectType <> 2 Then Me.cboCOM.SelectedIndex = Me.cboCOM.FindStringExact("COM" & _pi.ComNumber.ToString())
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
        If _pi.connectType = 2 Then
            Me.txtIPAddress.Text = _pi.Phone
            Me.txtIPPort.Text = _pi.ComNumber
        End If

        If _pi.connectType = 0 Then '拨号方式
            Me.optDial.PerformClick()
        ElseIf _pi.connectType = 1 Then '直连方式
            Me.optCOM.PerformClick()
        Else 'TCP方式
        End If
    End Sub


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        Dim pi As ProjectItem = Nothing

        If _pi Is Nothing Then
            pi = New ProjectItem()
        Else
            pi = _pi
        End If

        pi.ProjectName = Me.txtProjectName.Text.Trim
        pi.Remark = Me.txtRemark.Text.Trim
        'pi.ComNumber = Me.nudCOM.Value
        pi.Phone = Me.txtIPAddress.Text.Trim
        pi.ComNumber = CInt(Me.txtIPPort.Text)

        If Me.optDial.Checked Then
            pi.connectType = 0
        ElseIf Me.optCOM.Checked Then
            pi.connectType = 1
        Else
            pi.connectType = 2
        End If

        If _pi Is Nothing Then
            Try
                ProjectService.CreateBasicInfo(pi)
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("增加工程成功。")

        Else
            Try
                ProjectService.UpdateBasicInfo(pi)
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("修改工程成功。")

        End If

        SaveConnectConfig(pi) 'Add by ZhangTeng

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

    Private Function CheckValidate() As Boolean
        If Me.txtProjectName.Text.Trim = "" Then
            msg.Show("工程名称不能为空。")
            Me.txtProjectName.Focus()
            Return False
        End If


        'If optTcp.Checked Then
        '    For Each p As String In _ConnectParameters
        '        If p Is Nothing Then
        '            msg.ShowError("请配置连接参数")
        '            Me.btnConnectConfig.Focus()
        '            Return False
        '        End If
        '    Next
        'End If

        Return True
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub optCOM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCOM.CheckedChanged
        If Me.optCOM.Checked Then
            Me.pnlCOM.Enabled = True
            Me.pnlCOM.Visible = True
            Me.pnlTCP.Enabled = False
            Me.pnlTCP.Visible = False
            Me.txtIPAddress.Clear()
            Me.txtIPPort.Clear()
        End If
    End Sub

    Private Sub optDial_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDial.CheckedChanged
        If Me.optDial.Checked Then
            Me.pnlCOM.Enabled = True
            Me.pnlCOM.Visible = True
            Me.pnlTCP.Enabled = False
            Me.pnlTCP.Visible = False
            Me.txtIPAddress.Clear()
            Me.txtIPPort.Clear()
        End If
    End Sub

    Private Sub optTcp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       
    End Sub

    Private Sub txtIPPort_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIPPort.KeyPress
        e.Handled = False
        If Regex.IsMatch(e.KeyChar, "^\.$") OrElse (Not Char.IsDigit(e.KeyChar) AndAlso Asc(e.KeyChar) <> Keys.Back AndAlso Asc(e.KeyChar) <> Keys.Delete) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtIPAddress_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIPAddress.KeyPress
        e.Handled = False
        If Not Char.IsNumber(e.KeyChar) AndAlso Asc(e.KeyChar) <> Keys.Back AndAlso Asc(e.KeyChar) <> Keys.Delete Then
            e.Handled = True
        End If
    End Sub

    Private Sub btnConnectConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectConfig.Click
        Dim frm As New ConnectConfigForm(_ConnectParameters)
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _isChangedCP = True
        End If
    End Sub

    '-----------------2008-11-21--------------------
    '----------------连接参数设置-------------------
    Private _ConnectParameters(2) As String
    Private _isChangedCP As Boolean = False
    Private Sub LoadConnectConfig(ByVal pi As ProjectItem)
        _ConnectParameters(0) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_IdleTime")
        _ConnectParameters(1) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_WaitTime")
        _ConnectParameters(2) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_Rep")
    End Sub
    Private Sub SaveConnectConfig(ByVal pi As ProjectItem)
        If _isChangedCP Then
            KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_IdleTime", _ConnectParameters(0))
            KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_WaitTime", _ConnectParameters(1))
            KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_Rep", _ConnectParameters(2))
        End If
    End Sub
    '---------------Add by ZhangTeng-----------------
End Class